/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Seguridad.Vistas;

import Seguridad.Controlador.Validacion;
import Seguridad.Controlador.controladorseguridad;
import java.util.Date;
import javax.swing.JOptionPane;
import modelos.Usuario;


/**
 *
 * @author Saori
 */
public class ModificarContrasena extends javax.swing.JInternalFrame {

    /**
     * Creates new form ModificarContrasena
     */
    
    public static controladorseguridad mycontroladorseguridad = new controladorseguridad();
    public static Usuario usuario = lbj.LBJ.Login.usuario;
    public static Validacion validacion = new Validacion();

    public ModificarContrasena() {
        initComponents();
        loadComponents();
    }
    
    void loadComponents(){
        txtNombreUsuario.setText(usuario.getNombreusuario());
//        btnGuardar.setEnabled(false);
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        txtNombreUsuario = new javax.swing.JTextField();
        txtPasswordActual = new javax.swing.JPasswordField();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        btnGuardar = new javax.swing.JButton();
        txtPasswordNuevo1 = new javax.swing.JPasswordField();
        txtPasswordNuevo2 = new javax.swing.JPasswordField();
        btnCancelar = new javax.swing.JButton();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();

        setClosable(true);
        setTitle("Modificar Contraseña");

        jLabel1.setText("Nombre de Usuario");

        txtNombreUsuario.setEditable(false);

        txtPasswordActual.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPasswordActualActionPerformed(evt);
            }
        });

        jLabel2.setText("Contraseña Actual");

        jLabel3.setText("Nueva Contraseña");

        jLabel4.setText("Confirmar Contraseña");

        btnGuardar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/save.png"))); // NOI18N
        btnGuardar.setPreferredSize(new java.awt.Dimension(45, 45));
        btnGuardar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnGuardarActionPerformed(evt);
            }
        });

        txtPasswordNuevo1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPasswordNuevo1ActionPerformed(evt);
            }
        });
        txtPasswordNuevo1.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtPasswordNuevo1FocusLost(evt);
            }
        });

        txtPasswordNuevo2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPasswordNuevo2ActionPerformed(evt);
            }
        });
        txtPasswordNuevo2.addFocusListener(new java.awt.event.FocusAdapter() {
            public void focusLost(java.awt.event.FocusEvent evt) {
                txtPasswordNuevo2FocusLost(evt);
            }
        });

        btnCancelar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/imagenes/error 1.png"))); // NOI18N
        btnCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnCancelarActionPerformed(evt);
            }
        });

        jLabel12.setText("Cancelar");

        jLabel13.setText("Guardar");

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(44, 44, 44)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel1)
                            .add(jLabel2)
                            .add(jLabel3)
                            .add(jLabel4))
                        .add(87, 87, 87)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(txtNombreUsuario, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, 101, Short.MAX_VALUE)
                            .add(txtPasswordActual)
                            .add(txtPasswordNuevo1)
                            .add(txtPasswordNuevo2)))
                    .add(layout.createSequentialGroup()
                        .add(109, 109, 109)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                            .add(jLabel13)
                            .add(btnGuardar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                        .add(86, 86, 86)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel12)
                            .add(btnCancelar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))))
                .add(79, 79, 79))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .add(28, 28, 28)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(txtNombreUsuario, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel1))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(txtPasswordActual, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(jLabel2))
                .add(16, 16, 16)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                    .add(jLabel3)
                    .add(txtPasswordNuevo1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(14, 14, 14)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(jLabel4)
                    .add(txtPasswordNuevo2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .add(18, 18, 18)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                    .add(btnCancelar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 46, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                    .add(btnGuardar, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(jLabel12)
                    .add(jLabel13))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btnGuardarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnGuardarActionPerformed
        // TODO add your handling code here:
        if (JOptionPane.showConfirmDialog(rootPane, "¿Desea realmente cambiar su contraseña?",
        "Mensaje", JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION) {
            String passactual=new String(txtPasswordActual.getPassword());
            String passnuevo1=new String(txtPasswordNuevo1.getPassword());
            String passnuevo2=new String(txtPasswordNuevo2.getPassword());

    //        if(passnuevo1!=passnuevo2){
    //            JOptionPane.showMessageDialog(null, "No coinciden las contraseñas");
    //        }
            if(!passactual.equals(usuario.getPassword())){
                JOptionPane.showMessageDialog(null, "Contraseña actual incorrecta. Ingrese de nuevo los datos.");
                txtPasswordActual.setText("");
                txtPasswordNuevo1.setText("");
                txtPasswordNuevo2.setText("");
            }
            else{
                if(!validacion.es_clave(passnuevo1)){
    //            if(passnuevo1.length()<8){
                    JOptionPane.showMessageDialog(null, validacion.msj);
    //                JOptionPane.showMessageDialog(null, "La nueva contraseña debe tener como mínimo 8 caracteres.");
                    txtPasswordNuevo1.setText("");
                    txtPasswordNuevo2.setText("");
                }
                else if(!passnuevo1.equals(passnuevo2)){
                    JOptionPane.showMessageDialog(null, "No coinciden las nuevas contraseñas. Ingrese de nuevo los datos.");
                    txtPasswordNuevo1.setText("");
                    txtPasswordNuevo2.setText("");
                }
                else{
                    mycontroladorseguridad.editarContrasena(usuario, passactual, passnuevo1);
                    JOptionPane.showMessageDialog(null, "Su contraseña se actualizó existosamente.");
                    mycontroladorseguridad.insertarlog(usuario, new Date(), "Editar", "Usuario", "Editar Contraseña de Usuario");
                    this.dispose();
                }
            }
        }
//        this.setVisible(false);
//        Escritorio.buscarUsuario.setVisible(true);
    }//GEN-LAST:event_btnGuardarActionPerformed

    private void txtPasswordNuevo1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPasswordNuevo1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPasswordNuevo1ActionPerformed

    private void txtPasswordNuevo2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPasswordNuevo2ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPasswordNuevo2ActionPerformed

    private void txtPasswordActualActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPasswordActualActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPasswordActualActionPerformed

    private void txtPasswordNuevo2FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPasswordNuevo2FocusLost
//        // TODO add your handling code here:
//        String passnuevo1=new String(txtPasswordNuevo1.getPassword());
//        String passnuevo2=new String(txtPasswordNuevo2.getPassword());
//        
//        if(!passnuevo1.equals(passnuevo2)){
//            txtErrorIgualdad.setVisible(true);
//        }
//        else{
//            txtErrorIgualdad.setVisible(false);
//        }
//        
    }//GEN-LAST:event_txtPasswordNuevo2FocusLost

    private void txtPasswordNuevo1FocusLost(java.awt.event.FocusEvent evt) {//GEN-FIRST:event_txtPasswordNuevo1FocusLost
//        // TODO add your handling code here:
//        String passnuevo1=new String(txtPasswordNuevo1.getPassword());
//        
//        if(passnuevo1.length()<8){
//            txtErrorCantidad.setVisible(true);
//        }
//        else{
//            txtErrorCantidad.setVisible(false);
//        }
    }//GEN-LAST:event_txtPasswordNuevo1FocusLost

    private void btnCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnCancelarActionPerformed
        // TODO add your handling code here:
        if(JOptionPane.showConfirmDialog(null, "¿Está seguro de cancelar la actualización de contraseña?",
            "Mensaje",JOptionPane.YES_NO_OPTION) == JOptionPane.YES_OPTION){
            this.dispose();
        }
    }//GEN-LAST:event_btnCancelarActionPerformed

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnCancelar;
    private javax.swing.JButton btnGuardar;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JTextField txtNombreUsuario;
    private javax.swing.JPasswordField txtPasswordActual;
    private javax.swing.JPasswordField txtPasswordNuevo1;
    private javax.swing.JPasswordField txtPasswordNuevo2;
    // End of variables declaration//GEN-END:variables
}
